package org.display.util
{
	import flash.filesystem.File;
	import flash.utils.*;
	
	import org.data.*;
	import org.data.HashMap;
	import org.net.*;
	import org.utils.*;
	
	
	public class PublicJsfl
	{
		var paths:Array=[]
		
		var pathMap:HashMap=new HashMap()
		
		var items:Array=[]	
			
		var modulepath:String
		
		var jsfls:Array=[]
		
		public function PublicJsfl(path:String)
		{
			modulepath=path.replace(/file:\/\/\//,"")
				
			loadAllxml()

		}
		
		//第一步导入所有xml
		function loadAllxml(){
			
			FileControl.readFile(paths,Global.workspace+"\\xml")
				
			loadxml(paths.concat())
			
		}
		
		function loadxml(paths:Array){
			
			var res:AbcResponder=new AbcResponder()
			
			res.onResult=function(e,path){
				
				pathMap.put(path, e)
				
				if(paths.length>0){
					
					loadxml(paths)
					
				}else{
					
					jsfls=getChildren(modulepath)
					
					//trace(jsfls)
					
					if(jsfls.length>0)doJsfl(jsfls.shift())
					
				}
				
			}
			
			res.onFault=function(e,path){
				
				trace(e,path)
				
			}
			
			var xmlpath:String=paths.shift().replace(/file:\/\/\//,"")

			new AbcHTTPServices(xmlpath,res)
			
		}
		
		//获取每个xml的所有子项
		
		function createItems(){
			
			for(var i=0;i<paths.length;i++){
				
				items.push(getChildren(paths[i]))
				
			}
			
		}
		
		//获取一个模块所有的子模块
		function getChildren(path:String):Array{
			
			var childs:Array=[]
			
			createItem(path)
			
			childs.reverse()
			
			return childs
			
			function createItem(path:String){

				var source:XML=XML(pathMap.getValue(path))

				childs.push(path)
				
				var values:XMLList=source.item

				for(var i=0;i<values.length();i++){
					
				//	trace(values[i].path+"/////////////")
					
					if(values[i].path!=undefined){
						
				       createItem((Global.workspace+values[i].path.toString()).replace(/\\/gis,"/"))
					
					}

				}
				
			}
		}
		
		
		
		function doJsfl(path:String){
			
			var jsflPath:String=path.replace(/\//gis,"\\").replace(/\\xml\\/gis,"\\jsfl\\").replace(".xml",".jsfl")
				
		   //trace(jsflPath+"???????????????????")
			
			var file:File = File.desktopDirectory.resolvePath(jsflPath);
			file.openWithDefaultApplication();
			
			if(jsfls.length>0)setTimeout(doJsfl,2000,jsfls.shift())
			
		}
		
		
	}
}